home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Utilities / ODMemory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  11.4 KB  |  400 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ODMemory.cpp
  3.  
  4.     Contains:    Procedural implementation to the Memory component
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <4>     9/17/96    RA        1281338: ODNewHandle declaration mismatch
  13.                                     in ODMemory.cpp and ODMemory.h
  14.          <3>     6/19/96    jpa        Fixed header comment
  15.          <2>     6/19/96    jpa        1357406: Added COverridingMacOSMemory.
  16.  
  17.     In Progress:
  18.         
  19. */
  20.  
  21. #ifndef _ODMEMORY_
  22. #include "ODMemory.h"
  23. #endif
  24.  
  25. #ifndef _MEMMGR_
  26. #include "MemMgr.h"
  27. #endif
  28.  
  29. #ifndef __CODEFRAGMENTS__
  30. #include <CodeFragments.h>
  31. #endif
  32.  
  33. #ifndef __ERRORS__
  34. #include <Errors.h>
  35. #endif
  36.  
  37. #ifndef _EXCEPT_
  38. #include "Except.h"
  39. #endif
  40.  
  41. #ifndef _ODDEBUG_
  42. #include "ODDebug.h"
  43. #endif
  44.  
  45. #ifndef _UTILERRS_
  46. #include "UtilErrs.h"
  47. #endif
  48.  
  49. //========================================================================================
  50. // Constants
  51. //========================================================================================
  52.  
  53. const size_t kSystemHeapInitialSize = 20 * 1024;
  54. const size_t kSystemHeapGrowBySize  = 32 * 1024;
  55.  
  56. const ODSize kMinAppHeapSpace        = 16 * 1024;
  57. const ODSize kMinContigAppHeapSpace =  6 * 1024;
  58.  
  59. //========================================================================================
  60. // Global variables
  61. //========================================================================================
  62.  
  63. static ODMemoryHeapID    gDefaultHeap;
  64. #if 0
  65. #pragma lib_export on                // Need to tell compiler that gSystemHeap is imported
  66. extern void* gSystemHeap;            // Lives in Shared Globals library, MemShard.cpp.
  67. #pragma lib_export off
  68. #endif /* 0 */
  69. //========================================================================================
  70. // Initialization
  71. //========================================================================================
  72.  
  73.  
  74. OSErr InitODMemory( )
  75. {
  76.     gDefaultHeap = MMGetDefaultHeap();
  77.     return gDefaultHeap ?noErr :memFullErr;
  78. }
  79.  
  80. // Note: CFMInit stuff is now in UtilInit.cpp
  81.  
  82. //extern "C" pascal OSErr UtilitiesCFMInit( CFragInitBlockPtr );
  83.  
  84. // For use inside OpenDoc only. Parts won't need to use this but
  85. // should call InitODMemory from their own CFMInit routines.
  86. //pascal OSErr UtilitiesCFMInit (CFragInitBlockPtr /*initBlkPtr*/)
  87. //{
  88. //    return InitODMemory();
  89. //}
  90.  
  91.  
  92. //========================================================================================
  93. // Function declarations for operations on pointer based blocks
  94. //========================================================================================
  95.  
  96.  
  97. //----------------------------------------------------------------------------------------
  98. // ODGetDefaultHeap
  99. //----------------------------------------------------------------------------------------
  100.  
  101. ODMemoryHeapID ODGetDefaultHeap()
  102. {
  103.     ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  104.     return gDefaultHeap;
  105. }
  106. #if 0
  107. //----------------------------------------------------------------------------------------
  108. // ODGetSystemHeap
  109. //----------------------------------------------------------------------------------------
  110.  
  111. ODMemoryHeapID ODGetSystemHeap()
  112. {
  113.     // The system heap should be created only by the OD system process.
  114.     
  115.     if( !gSystemHeap ) {
  116.         gSystemHeap = ODCreateHeap(    kSystemHeapInitialSize,
  117.                                     kSystemHeapGrowBySize,
  118.                                     kODTrue,"OpenDoc System Heap" );
  119.         THROW_IF_NULL(gSystemHeap);
  120.     }
  121.     return (ODMemoryHeapID)gSystemHeap;
  122. }
  123. #endif /* 0 */
  124. //----------------------------------------------------------------------------------------
  125. // ODCreateHeap
  126. //----------------------------------------------------------------------------------------
  127.  
  128. ODMemoryHeapID ODCreateHeap(unsigned long sizeInitial, unsigned long sizeIncrement,
  129.                             Boolean fromSysMemory, const char *name)
  130. {
  131.     MemHeap *heap = MMNewHeap(fromSysMemory ?kMMSysMemory :kMMTempMemory,
  132.                               sizeInitial, sizeIncrement, name);
  133.     if( !heap )
  134.         THROW(kODErrOutOfMemory);
  135.     return heap;
  136. }
  137.  
  138. //----------------------------------------------------------------------------------------
  139. // ODDestroyHeap
  140. //----------------------------------------------------------------------------------------
  141.  
  142. void ODDestroyHeap(ODMemoryHeapID heapID)
  143. {
  144.     MMDisposeHeap(heapID);
  145. }
  146.  
  147. //----------------------------------------------------------------------------------------
  148. // ODNewPtr
  149. //----------------------------------------------------------------------------------------
  150.  
  151. void *ODNewPtr(ODBlockSize blkSize, ODMemoryHeapID heapID)
  152. {
  153.     if( heapID == kODNULL ) {
  154.         ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  155.         heapID = gDefaultHeap;
  156.     }
  157.     void *block = MMAllocateIn(blkSize,heapID);
  158.     if( !block )
  159.         THROW(kODErrOutOfMemory);
  160.     return block;
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. // ODNewPtrClear
  165. //----------------------------------------------------------------------------------------
  166.  
  167. void *ODNewPtrClear(ODBlockSize blkSize, ODMemoryHeapID heapID)
  168. {
  169.     if( heapID == kODNULL ) {
  170.         ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  171.         heapID = gDefaultHeap;
  172.     }
  173.     void *block = MMAllocateClearIn(blkSize,heapID);
  174.     if( !block )
  175.         THROW(kODErrOutOfMemory);
  176.     return block;
  177. }
  178.  
  179. //----------------------------------------------------------------------------------------
  180. // ODReallocate
  181. //----------------------------------------------------------------------------------------
  182.  
  183. void *ODReallocate(void *block, ODBlockSize newSize)
  184. {
  185.     block = MMReallocate(block,newSize);
  186.     if( !block && newSize>0 )
  187.         THROW(kODErrOutOfMemory);
  188.     return block;
  189. }
  190.  
  191. //------------------------------------------------------------------------------
  192. // ODDisposePtr
  193. //------------------------------------------------------------------------------
  194.  
  195. void ODDisposePtr(void *pointer)
  196. {
  197.     MMFree(pointer);
  198. }
  199.  
  200. //----------------------------------------------------------------------------------------
  201. // ODRecoverHeapID
  202. //----------------------------------------------------------------------------------------
  203.  
  204. ODMemoryHeapID ODRecoverHeapID(const void *block)
  205. {
  206.     ODMemoryHeapID heap = MMGetHeap(block);
  207.     if( !heap )
  208.         THROW(kODErrInvalidBlock);
  209.     return heap;
  210. }
  211.  
  212.  
  213.  
  214. //----------------------------------------------------------------------------------------
  215. // ODBlockIsObject
  216. //----------------------------------------------------------------------------------------
  217.  
  218. void ODBlockIsObject( void *block, ODBoolean isObject )
  219. {
  220.     MMSetIsObject(block,isObject);
  221. }
  222.  
  223.  
  224. //----------------------------------------------------------------------------------------
  225. // ODIsBlockAnObject
  226. //----------------------------------------------------------------------------------------
  227.  
  228. ODBoolean ODIsBlockAnObject( const void *block )
  229. {
  230.     return MMIsObject(block);
  231. }
  232.  
  233.  
  234. //========================================================================================
  235. // Function declarations for operations on handle based blocks
  236. //========================================================================================
  237.  
  238. //----------------------------------------------------------------------------------------
  239. // ODNewHandle
  240. //----------------------------------------------------------------------------------------
  241.  
  242. ODHandle ODNewHandle(ODBlockSize howBig)
  243. {
  244.     ODHandle h = (ODHandle) MMAllocateHandleIn(howBig,kMMTempMemory);
  245.     if( !h )
  246.         THROW(kODErrOutOfMemory);
  247.     return h;
  248.         
  249. }
  250.  
  251. //----------------------------------------------------------------------------------------
  252. // ODDisposeHandle
  253. //----------------------------------------------------------------------------------------
  254.  
  255. void ODDisposeHandle(ODHandle handle)
  256. {
  257.     MMFreeHandle((MMHandle)handle);
  258. }
  259.  
  260. //----------------------------------------------------------------------------------------
  261. // ODCopyHandle
  262. //----------------------------------------------------------------------------------------
  263.  
  264. ODHandle ODCopyHandle(ODHandle handle)
  265. {
  266.     ODHandle h = (ODHandle) MMCopyHandle((MMHandle)handle);
  267.     if( !h )
  268.         THROW(kODErrOutOfMemory);
  269.     return h;
  270. }
  271.  
  272. //----------------------------------------------------------------------------------------
  273. // ODGetHandleSize(ODHandle handle)
  274. //----------------------------------------------------------------------------------------
  275.  
  276. ODULong ODGetHandleSize(ODHandle handle)
  277. {
  278.     return MMGetHandleSize((MMHandle)handle);
  279. }
  280.  
  281. //----------------------------------------------------------------------------------------
  282. // ODSetHandleSize(ODHandle handle, ODULong blkSize)
  283. //----------------------------------------------------------------------------------------
  284.  
  285. void ODSetHandleSize(ODHandle handle, ODULong blkSize)
  286. {
  287.     if( !MMSetHandleSize((MMHandle)handle,blkSize) )
  288.         THROW(kODErrOutOfMemory);
  289.  
  290. }
  291.  
  292. //----------------------------------------------------------------------------------------
  293. // ODLockHandle(ODHandle handle)
  294. //----------------------------------------------------------------------------------------
  295.  
  296. void* ODLockHandle(ODHandle handle)
  297. {
  298.     void *p = MMLockHandle((MMHandle)handle);
  299.     if( !p )
  300.         THROW(kODErrOutOfMemory);
  301.     return p;
  302. }
  303.  
  304. //----------------------------------------------------------------------------------------
  305. // ODUnlockPtr(void* ptr)
  306. //----------------------------------------------------------------------------------------
  307.  
  308. void ODUnlockPtr(void* ptr)
  309. {
  310.     MMUnlockPtr(ptr);
  311. }
  312.  
  313. //----------------------------------------------------------------------------------------
  314. // ODUnlockHandle(ODHandle handle)
  315. //----------------------------------------------------------------------------------------
  316.  
  317. void ODUnlockHandle(ODHandle handle)
  318. {
  319.     MMUnlockHandle((MMHandle)handle);
  320. }
  321.  
  322.  
  323. //========================================================================================
  324. // Function declarations utility functions
  325. //========================================================================================
  326.  
  327. //------------------------------------------------------------------------------
  328. // ODBlockMove
  329. //------------------------------------------------------------------------------
  330.  
  331.  
  332. void ODBlockMove( const void *from, void *to, ODULong size)
  333. {
  334.     MMMove(to,from,size);
  335. }
  336.  
  337.  
  338. //------------------------------------------------------------------------------
  339. // ODNewRgn
  340. //------------------------------------------------------------------------------
  341.  
  342. RgnHandle ODNewRgn( )
  343. {
  344.     RgnHandle r = (RgnHandle) ODNewHandle(sizeof(Region));
  345.     (**r).rgnSize = sizeof(Region);
  346.     SetRect(&(**r).rgnBBox, 0,0,0,0);
  347.     return r;
  348. }
  349.  
  350. //------------------------------------------------------------------------------
  351. // ODRequireFreeSpace
  352. //------------------------------------------------------------------------------
  353.  
  354.  
  355. ODBoolean
  356. ODHaveFreeSpace( ODSize haveTotal, ODSize haveContig /*=0*/,
  357.                  ODBoolean appHeap /*=false*/ )
  358. {
  359.     size_t total, contig;
  360.     if( appHeap )
  361.         MMSystemFreeSpace(kMMAppMemory, &total,&contig);
  362.     else
  363.         MMGetFreeSpace(gDefaultHeap,&total,&contig );
  364.     return total>=haveTotal && contig>=haveContig;
  365. }
  366.  
  367. void
  368. ODRequireFreeSpace( ODSize total, ODSize contig, ODBoolean appHeap )
  369. {
  370.     if( !ODHaveFreeSpace(total,contig,appHeap) )
  371.         THROW(appHeap ?memFullErr :kODErrOutOfMemory);
  372.     
  373.     // I decided it made sense to throw a Mac mem mgr error if the
  374.     // app heap is full, since this is the error you'd get if you
  375.     // tried to d
  376. }
  377.  
  378. void
  379. ODCheckAppHeapSpace( )
  380. {
  381.     if( !ODHaveFreeSpace(kMinAppHeapSpace,kMinContigAppHeapSpace,kODTrue) )
  382.         THROW(memFullErr);
  383. }
  384.  
  385.  
  386. //========================================================================================
  387. // MacOS NewHandle override
  388. //========================================================================================
  389.  
  390. COverridingMacOSMemory::COverridingMacOSMemory( )
  391. {
  392.     MMOverridePlatform(kODTrue,kODTrue);
  393. }
  394.  
  395.  
  396. COverridingMacOSMemory::~COverridingMacOSMemory( )
  397. {
  398.     MMEndOverridePlatform(kODTrue,kODTrue);
  399. }
  400.